package org.tnc.budget.webapp.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.tnc.budget.model.BudgetCenter;
import org.tnc.budget.service.BudgetManager;

/**
 * 与预算中心相关功能的web接入程序
 * 
 * 1. 查询预算中心列表
 * 
 * @author houjj | 2011-4-3 下午04:14:23
 */
@Controller
public class BudgetCenterController {
	private Logger log = Logger.getLogger(BudgetCenterController.class);

	private BudgetManager budgetManager;

	@RequestMapping(value = "/budget.do")
	public void getBudgetList(HttpServletRequest request,
			HttpServletResponse response) {

		if (log.isDebugEnabled())
			log.debug("get budget list start ...");

		StringBuffer res = new StringBuffer();
		List<BudgetCenter> budgetCenters = budgetManager.getAll();
		if (budgetCenters != null) {
			// 返回预算中心的名称，以“，”分隔
			int len = 0;
			for (BudgetCenter budgetCenter : budgetCenters) {
				res.append(budgetCenter.getBcName());
				
				len ++;
				
				if(len < budgetCenters.size()) 
					res.append(",");
			}
		}

		// 如果无预算中心，返回-1
		String result = res.toString();
		if ("".equals(result)) {
			result = "-1";
		}

		try {
			PrintWriter out = response.getWriter();
			out.write(result);
			out.close();

		} catch (IOException e) {
			e.printStackTrace();
			log.error("Get budget center info list failed ", e);
		}

	}

	public void setBudgetManager(BudgetManager budgetManager) {
		this.budgetManager = budgetManager;
	}

}
